package com.yld.car.common;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class ImageUtils {
	public static final String AVATAR_LARGE = "200";
	public static final String AVATAR_MIDDLE = "90";
	public static final String AVATAR_SMALL = "50";

	public static byte[] bitmapToByte(Bitmap paramBitmap) {
		if (paramBitmap == null)
			return null;
		ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
		paramBitmap.compress(Bitmap.CompressFormat.PNG, 50,
				localByteArrayOutputStream);

		return localByteArrayOutputStream.toByteArray();
	}

	public static Drawable bitmapToDrawable(Bitmap paramBitmap) {
		if (paramBitmap == null)
			return null;
		return new BitmapDrawable(paramBitmap);
	}

	public static Bitmap getBitmapFromResources(Activity paramActivity,
			int paramInt) {
		return BitmapFactory.decodeResource(paramActivity.getResources(),
				paramInt);
	}

	public static Bitmap imageZoom(Bitmap paramBitmap, double paramDouble) {
		ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
		paramBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
				localByteArrayOutputStream);
		double d1 = localByteArrayOutputStream.toByteArray().length / 1024;
		if (d1 > paramDouble) {
			double d2 = d1 / paramDouble;
			paramBitmap = zoomImage(paramBitmap,
					paramBitmap.getWidth() / Math.sqrt(d2),
					paramBitmap.getHeight() / Math.sqrt(d2));
		}
		return paramBitmap;
	}

	public static Bitmap imageZoomWidth(Bitmap paramBitmap) {
		ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
		paramBitmap.compress(Bitmap.CompressFormat.JPEG, 90,
				localByteArrayOutputStream);
		if (paramBitmap.getWidth() > 500)
			paramBitmap = zoomImage(paramBitmap, 500.0D,
					500 * paramBitmap.getHeight() / paramBitmap.getWidth());
		return paramBitmap;
	}

	public static Bitmap loadBitmap(String paramString,
			BitmapFactory.Options paramOptions) {
		return BitmapFactory.decodeFile(paramString, paramOptions);
	}

	public static Bitmap zoomImage(Bitmap paramBitmap, double paramDouble1,
			double paramDouble2) {
		float f1 = paramBitmap.getWidth();
		float f2 = paramBitmap.getHeight();
		Matrix localMatrix = new Matrix();
		localMatrix.postScale((float) paramDouble1 / f1, (float) paramDouble2
				/ f2);
		return Bitmap.createBitmap(paramBitmap, 0, 0, (int) f1, (int) f2,
				localMatrix, true);
	}

	public static Bitmap convertBitmapByPathName(String pathName) {

		try {
			FileInputStream fis = new FileInputStream(pathName);
			Bitmap bitmap = BitmapFactory.decodeStream(fis);
			bitmap = Bitmap.createScaledBitmap(bitmap, 300, 300, false);
			return bitmap;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
